package com.xiaomiMall.mange.controller.notable;


import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.xiaomiMall.mange.domain.Notable;
import com.xiaomiMall.mange.service.NotableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/xiaomi-mall/notable")
public class Notable_1Controller extends BaseController {

    @Autowired
    private NotableService notableService;

    // 查询所有图片
    @GetMapping("/list")
    public TableDataInfo list()
    {
        startPage();
        List<Notable> list = notableService.selectAll();
        return getDataTable(list);
    }

    // 删除图片
    @DeleteMapping("/{notableIds}")
    public AjaxResult remove(@PathVariable Long[] notableIds)
    {
        return toAjax(notableService.deleteNotableByIds(notableIds));
    }


    // 新增图片
    @PostMapping("/addPicture")
    public AjaxResult add(@RequestParam("file") MultipartFile file, Long notableProperty) throws IOException {

        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            System.out.println(avatar);
            Notable notable1 = new Notable();
            notable1.setNotableProperty(notableProperty);
            notable1.setNotableAddress(avatar);
            if (notableService.insertNotable(notable1) != 0) {
                AjaxResult ajax = AjaxResult.success();
                return ajax;
            }
        }
        return AjaxResult.error("添加图片异常，请联系管理员");
    }

    //更换图片
    @PostMapping("updatePicture")
    public AjaxResult update(@RequestParam("file") MultipartFile file,Long notableId) throws IOException{

        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file);
            System.out.println(avatar);
            Notable notable1 = new Notable();
            notable1.setNotableId(notableId);
            notable1.setNotableAddress(avatar);
            if (notableService.updateNotable(notable1) != 0 ) {
                AjaxResult ajax = AjaxResult.success();
                return ajax;
            }
        }
        return AjaxResult.error("更换图片异常，请联系管理员");
    }

}
